package com.caifeng.codegen;


import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.core.keygen.KeyGenerators;
import com.zaxxer.hikari.HikariDataSource;
import com.mybatisflex.codegen.config.GlobalConfig;

import java.util.Scanner;

public class Codegen {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入数据库名称：");
        String dbName = scanner.nextLine();

        System.out.println("请输入模块名：");
        String modelName = scanner.nextLine();

        System.out.println("请输入包名：");
        String basePackage = scanner.nextLine();

        System.out.println("请输入表名，多个以英文逗号分隔：");
        String[] tables = scanner.nextLine().split(",");

        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://" + System.getenv("VM_HOST") +":3306/" + dbName + "?characterEncoding=utf-8");
        dataSource.setUsername("root");
        dataSource.setPassword(System.getenv("PASSWORD"));

        //创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfig(modelName, basePackage, tables);

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        //生成代码
        generator.generate();
    }

    public static GlobalConfig createGlobalConfig(String modelName, String basePackage, String... tables) {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        globalConfig.setSourceDir(System.getProperty("user.dir") + "/" + modelName + "/src/main/java");
        globalConfig.setMapperXmlPath(System.getProperty("user.dir") + "/" + modelName + "/src/main/resources/mapper");

        //设置根包
        globalConfig.setBasePackage(basePackage);

        //设置表前缀和只生成哪些表
        for (String table : tables) {
            globalConfig.setGenerateTable(table);
        }

        //设置项目的JDK版本，项目的JDK为14及以上时建议设置该项，小于14则可以不设置
        globalConfig.setEntityJdkVersion(17);

        //策略配置
        globalConfig.setLogicDeleteColumn("logic_delete");
        globalConfig.setVersionColumn("version");

        //设置生成 entity
        globalConfig.setEntityGenerateEnable(true);
        globalConfig.getEntityConfig()
                .setWithActiveRecord(true)
                .setWithLombok(true)
                .setOverwriteEnable(true);

        globalConfig.setTableDefGenerateEnable(false);
        globalConfig.getTableDefConfig()
                .setClassSuffix("Def")
                .setOverwriteEnable(true);

        //设置生成 mapper
        globalConfig.setMapperGenerateEnable(true);
        globalConfig.getMapperConfig()
                .setClassSuffix("Mapper")
                .setOverwriteEnable(true);

        globalConfig.setMapperXmlGenerateEnable(true);
        globalConfig.getMapperXmlConfig()
                .setFileSuffix("Mapper")
                .setOverwriteEnable(true);

        ColumnConfig idColumnConfig = new ColumnConfig();
        idColumnConfig.setColumnName("id")
                .setPrimaryKey(true)
                .setKeyType(KeyType.Generator)
                .setKeyValue(KeyGenerators.flexId);
        globalConfig.setColumnConfig(idColumnConfig);

        ColumnConfig insertColumnConfig = new ColumnConfig();
        insertColumnConfig.setColumnName("create_time")
                .setOnInsertValue("now()");
        globalConfig.setColumnConfig(insertColumnConfig);

        ColumnConfig updateColumnConfig = new ColumnConfig();
        updateColumnConfig.setColumnName("update_time")
                .setOnUpdateValue("now()");
        globalConfig.setColumnConfig(updateColumnConfig);

        return globalConfig;
    }
}